package com.sphd.beautysalon.model;


import com.android.volley.VolleyError;
import com.sphd.beautysalon.info.InvestorInfo;
import com.sphd.beautysalon.info.InvestorItemInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.interfaces.VolleyJsonObjectCallBack;
import com.sphd.beautysalon.presenter.FundingPersonPresenterImpl;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hu on 2016/12/29
 */

public class FundingPersonModelImpl implements FundingPersonModel, VolleyJsonObjectCallBack {
    FundingPersonPresenterImpl fundingPersonPresenter;
    RestaurantInfo restaurantInfo;
    @Override
    public void sendUrl(String url, FundingPersonPresenterImpl fundingPersonPresenter, RestaurantInfo restaurantInfo) {
        this.fundingPersonPresenter = fundingPersonPresenter;
        this.restaurantInfo=restaurantInfo;
        volleyUtil.setJsonObjectRequest(url, this);
    }

    @Override
    public void setAddOrCut(String url, FundingPersonPresenterImpl fundingPersonPresenter) {
        this.fundingPersonPresenter = fundingPersonPresenter;
        volleyUtil.setJsonObjectRequest(url, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    int res =response.getInt("res");
                    fundingPersonPresenter.recAddOrCut(res);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
        fundingPersonPresenter.sendError(error);
            }
        });
    }

    @Override
    public void onResponse(JSONObject response) {
        try {
            List<InvestorInfo> infoList = JsonUtil.getListFromString(response.getString("list"), InvestorInfo.class);
                fundingPersonPresenter.sendPerseonMsg(infoList);
            JSONArray listcg = response.getJSONArray("listcg");
            //获取店铺的地址和名称
            for (int i = 0; i < listcg.length(); i++) {
                JSONObject object = listcg.getJSONObject(i);
                if (object.has("cg_name")) {
                    restaurantInfo.setCg_name(object.getString("cg_name"));
                }
                if (object.has("cg_addr")) {
                    restaurantInfo.setCg_addr(object.getString("cg_addr"));
                }
            }
                fundingPersonPresenter.sendResInfo(restaurantInfo);
            //获取负责人的电话和姓名
            JSONArray listmanager = response.getJSONArray("listmanager");
            for (int i = 0; i < listmanager.length(); i++) {
                JSONObject object = listmanager.getJSONObject(i);
                if (object.has("staff_phone")) {
                    fundingPersonPresenter.manPhone(object.getString("staff_phone"));
                }
                if (object.has("staff_name")) {
                    fundingPersonPresenter.manName(object.getString("staff_name"));
                }
            }
            //最少投资天数
            if (response.has("account_floor_day")) {
                fundingPersonPresenter.floorDay(response.getLong("account_floor_day"));
            }
            //今日分红
            if (response.has("today")) {
                fundingPersonPresenter.todayMoney(response.getDouble("today"));
            }
            //千分率
            if (response.has("permillage")) {
                fundingPersonPresenter.permillage(response.getDouble("permillage"));
            }
            //总分红
            if (response.has("all_interest")) {
                fundingPersonPresenter.allInterest(response.getDouble("all_interest"));
            }
            //允许撤回的资金的金额
            if (response.has("limit")) {
                fundingPersonPresenter.limit(response.getDouble("limit"));
            }

            List<InvestorItemInfo> itemInfoList = new ArrayList<>();
            JSONArray jsonArray = response.getJSONArray("listyear");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                InvestorItemInfo investorItemInfo = new InvestorItemInfo();
                if (object.has("sum")) {
                    investorItemInfo.setmYearTurnover(object.getDouble("sum"));
                }
                if (object.has("SUM(account_interest)")) {
                    investorItemInfo.setmYearCut(object.getDouble("SUM(account_interest)"));
                }
                if (object.has("date_format(account_day, '%Y')")) {
                    investorItemInfo.setmYear(object.getString("date_format(account_day, '%Y')"));

                }
                itemInfoList.add(investorItemInfo);
            }
            fundingPersonPresenter.senditemInfoList(itemInfoList);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        fundingPersonPresenter.sendError(error);
    }
}